/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tp3;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Aurelie
 */

@Entity
public class CompteBancaire implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String nomProprietaire;
    private long soldeCompte;
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @OrderBy("dateOperation ASC")
    private Collection<OperationBancaire> operations = new ArrayList();  

    public CompteBancaire() {
    }

    /**
     * Constructeur
     */
    public CompteBancaire (String nomProprietaire, long soldeCompte){
        this.nomProprietaire = nomProprietaire;
        this.soldeCompte = soldeCompte;
        System.out.print("Je suis la");
        OperationBancaire op = new OperationBancaire("Création du compte", soldeCompte);          
        System.out.print("OP : " + op);
        addOperationBancaire(op); // ici on ajoute la relation  
            System.out.print(op.getId() + "" + op.getDescription() + "" + op.getDateOperation());
    }
    /**
     * Retirer argent
     * @param nom
     * @param solde 
     */
    public void retirerArgent(long montant){
        soldeCompte =  soldeCompte - montant; 
        OperationBancaire op = new OperationBancaire("Retrait", montant);
        addOperationBancaire(op); // ici on ajoute la relation  
 
    }
    
    /**
     * ajouterArgent
     * @param nom
     * @param solde 
     */
    public void ajouterArgent(long montant){
        soldeCompte = soldeCompte + montant;
         OperationBancaire op = new OperationBancaire("Versement", montant);
         addOperationBancaire(op); 
    }
  
        public Collection<OperationBancaire> getOperations() {  
        return operations;  
    }  
  
    public void setOperations(Collection<OperationBancaire> operations) {  
        this.setOperations(operations);  
    }  
  
    public void addOperationBancaire(OperationBancaire o) {  
        getOperations().add(o);  
    }  
    
     
    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CompteBancaire)) {
            return false;
        }
        CompteBancaire other = (CompteBancaire) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "tp3.CompteBanquaire[ id=" + id + " ]";
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the nomProprietaire
     */
    public String getNomProprietaire() {
        return nomProprietaire;
    }

    /**
     * @param nomProprietaire the nomProprietaire to set
     */
    public void setNomProprietaire(String nomProprietaire) {
        this.nomProprietaire = nomProprietaire;
    }

    /**
     * @return the soldeCompte
     */
    public long getSoldeCompte() {
        return soldeCompte;
    }

    /**
     * @param soldeCompte the soldeCompte to set
     */
    public void setSoldeCompte(long soldeCompte) {
        this.soldeCompte = soldeCompte;
    }

    
}
